package com.koron.order.risk.keyPoint.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseBean;
import com.koron.bean.query.PageQuery;
import com.koron.order.linepatrol.base.bean.query.PatrolQuery;
import com.koron.order.linepatrol.base.bean.vo.PatrolVo;
import com.koron.order.linepatrol.base.business.service.PatrolBusinessService;
import com.koron.order.risk.base.bean.convertor.LineRiskConvertor;
import com.koron.order.risk.base.bean.po.LineRiskDetailDefault;
import com.koron.order.risk.base.bean.po.LineRiskPart;
import com.koron.order.risk.base.service.LineRiskDetailDefaultService;
import com.koron.order.risk.base.service.LineRiskPartService;
import com.koron.order.risk.keyPoint.bean.dto.LineRiskKeyPointDTO;
import com.koron.order.risk.keyPoint.bean.po.LineRiskKeyPoint;
import com.koron.order.risk.keyPoint.bean.query.LineRiskKeyPointQuery;
import com.koron.order.risk.keyPoint.bean.vo.LineRiskKeyPointVO;
import com.koron.order.risk.keyPoint.mapper.LineRiskKeyPointMapper;
import com.koron.order.risk.keyPoint.service.LineRiskKeyPointService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/12/30
 */
@Service
public class LineRiskKeyPointServiceImpl implements LineRiskKeyPointService {

    @Autowired
    private LineRiskDetailDefaultService lineRiskDetailDefaultService;

    @Autowired
    private PatrolBusinessService linePatrolBusinessService;

    @Autowired
    private LineRiskConvertor lineRiskConvertor;

    @Autowired
    private LineRiskPartService lineRiskPartService;

    @Override
    @TaskAnnotation("getById")
    public LineRiskKeyPointVO getById(SessionFactory factory, String id) {
        LineRiskKeyPointMapper mapper = factory.getMapper(LineRiskKeyPointMapper.class);
        LineRiskKeyPointVO lineRiskKeyPointVO = mapper.selectById(id);
        List<LineRiskPart> lineRiskPartList = lineRiskPartService.listByRiskId(factory, id);
        lineRiskKeyPointVO.setLineRiskPartList(lineRiskPartList);
        return lineRiskKeyPointVO;
    }

    @Override
    @TaskAnnotation("getDtoById")
    public LineRiskKeyPointDTO getDtoById(SessionFactory factory, String id) {
        LineRiskKeyPointVO lineRiskKeyPoint = getById(factory, id);
        LineRiskKeyPointDTO lineRiskKeyPointDTO = lineRiskConvertor.voToDto(lineRiskKeyPoint);

        // 查询巡视工单信息
        if (lineRiskKeyPoint.getRefPlan() == 1 && StrUtil.isNotEmpty(lineRiskKeyPoint.getPlanIds())) {
            PatrolQuery patrolQuery = new PatrolQuery();
            patrolQuery.setPlanIds(new ArrayList<>(Arrays.asList(lineRiskKeyPoint.getPlanIds().split(","))));
            PageQuery pageQuery = new PageQuery();
            pageQuery.setPage(1);
            pageQuery.setPageSize(100);
            PageInfo<PatrolVo> pageInfo = linePatrolBusinessService.queryVoPageList(factory, patrolQuery, pageQuery);

            List<PatrolVo> list = pageInfo.getList();
            lineRiskKeyPointDTO.setPatrolVoList(list);
        }
        return lineRiskKeyPointDTO;
    }

    @Override
    @TaskAnnotation("list")
    public List<LineRiskKeyPointVO> list(SessionFactory factory, LineRiskKeyPointQuery query) {
        LineRiskKeyPointMapper mapper = factory.getMapper(LineRiskKeyPointMapper.class);
        List<LineRiskKeyPointVO> lineRiskKeyPoints = mapper.selectList(query);
        if (CollUtil.isEmpty(lineRiskKeyPoints)) {
            return lineRiskKeyPoints;
        }
        List<String> ids = lineRiskKeyPoints.stream().map(BaseBean::getId).collect(Collectors.toList());
        List<LineRiskPart> lineRiskPartList = lineRiskPartService.listByRiskIds(factory, ids);
        if (CollUtil.isEmpty(lineRiskPartList)) {
            return lineRiskKeyPoints;
        }
        Map<String, List<LineRiskPart>> lineRiskPartMap = lineRiskPartList.stream().collect(Collectors.groupingBy(LineRiskPart::getRiskId, LinkedHashMap::new, Collectors.toList()));
        lineRiskKeyPoints.forEach(e -> e.setLineRiskPartList(lineRiskPartMap.get(e.getId())));
        return lineRiskKeyPoints;
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<LineRiskKeyPointVO> page(SessionFactory factory, PageQuery pageQuery, LineRiskKeyPointQuery query) {
        pageQuery.unPrefixOrders("lineRisk", Arrays.asList("planIds", "refPlan", "keyLevel", "keyPointRemark"));
        return LineRiskKeyPointService.super.page(factory, pageQuery, query);
    }

    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, LineRiskKeyPoint lineRiskKeyPoint) {
        // 将备注信息保存到LineRiskDetailDefault
        // ownerId作为riskId
        // remark作为keyPointRemark
        // riskId置null
        String riskId = lineRiskKeyPoint.getId();
        String keyPointRemark = lineRiskKeyPoint.getKeyPointRemark();
        LineRiskDetailDefault lineRiskDetailDefault = new LineRiskDetailDefault();
        lineRiskDetailDefault.setId(riskId);
        lineRiskDetailDefault.setOwnerId(riskId);
        lineRiskDetailDefault.setRemark(keyPointRemark);
        lineRiskDetailDefaultService.save(factory, lineRiskDetailDefault);
        return true;
    }

}
